﻿#region Copyright Notice
// This file is part of GestionPedago.
// <description>
// Copyright (C) 2012 ENI Ecole Informatique
// 
// GestionPedago is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// <program> is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GestionPedago.EntityContext;
using GestionPedago.EntityContext.EntityOPTools;

namespace GestionPedago.Persistence.OPManager
{
   public abstract class ActiviteParTitreOPManager
    {
       public static List<ActiviteParTitre> GetListeActiviteParTitre(string recherche, bool orderByLibelleAsc = true)
       {
           try
           {
               IQueryable<ActiviteParTitre> query = EntityConnection.GetContext().ActiviteParTitre;

               query.Where(it => (recherche == null || recherche.Trim() == "" || it.Activite.Libelle.ToUpper().Contains(recherche.ToUpper())));

               if (orderByLibelleAsc)
               {
                   query.OrderBy(it => it.Activite.Libelle);
               }
               else
               {
                   query.OrderByDescending(it => it.Activite.Libelle);
               }
               query.Select(it => it);

               return query.ToList();
           }
           catch (Exception ex)
           {
               throw ex;
           }
       }

       public static ActiviteParTitre GetActiviteParTitreById(int id)
       {

           try
           {
               var query = from it in EntityConnection.GetContext().ActiviteParTitre
                           where it.IdActiviteParTitre == id
                           select it;

               ActiviteParTitre c = null;

               c = query.First();

               return c;

           }
           catch (Exception ex)
           {
               throw ex;
           }

       }

       public static ActiviteParTitre GetActiviteParTitreByIdActiviteCodeTitre(int idActivite,string codeTitre)
       {

           try
           {
               var query = from it in EntityConnection.GetContext().ActiviteParTitre
                           where it.IdActivite == idActivite && it.CodeTitre == codeTitre
                           select it;

               ActiviteParTitre c = null;

               c = query.First();

               return c;

           }
           catch (Exception ex)
           {
               throw ex;
           }

       }



       public static ActiviteParTitre AjouterActiviteParTitre(ActiviteParTitre c)
       {
           try
           {
               if (!EntityStateChecker.IsDetached(c))
               {
                   EntityConnection.GetContext().ActiviteParTitre.Detach(c);
               }
               EntityConnection.GetContext().ActiviteParTitre.AddObject(c);
               EntityConnection.GetContext().SaveChanges();
               return c;
           }
           catch (Exception ex)
           {
               throw ex;
           }
       }

       public static void SupprimerActiviteParTitre(ActiviteParTitre c)
       {
           try
           {
               
               EntityConnection.GetContext().ActiviteParTitre.DeleteObject(c);
               EntityConnection.GetContext().SaveChanges();
           }
           catch (Exception ex)
           {
               throw ex;
           }

       }
    }
}
